package com.usc.entme;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * This is the main class which hosts the main screen for the application.
 * TabLayout displays personalized choices like "News" / "Sports" / "Movies"
 * 
 * @author pranav
 *
 */
public class EntmeActivity extends TabActivity {
	private static Context context;

	private static final int REQUEST_CODE = 1234;
	private String TAG="Voice";

	private int tabHostIndex;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = this;

		Resources resources = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;

		Bundle extras = this.getIntent().getExtras();
		if(extras == null)
		{
			tabHostIndex=0;
		}
		else{
			 tabHostIndex = extras.getInt("tabHostIndex",0);	
		}


		Intent intent;

		// We are going to initialize the TabContent for News

		intent = new Intent().setClass(this, News.class);
		//TODO -Place the proper image for news tab icon 
		//currently for the prototyping I have used the icon which I found on site.
		spec = tabHost.newTabSpec("news")
		.setIndicator("", resources.getDrawable(R.drawable.newstab))
		.setContent(intent);

		tabHost.addTab(spec);


		// We are initializing the TabContent for Movies

		intent = new Intent().setClass(this, Movies.class);
		//TODO - Place the proper image for movies tab icon
		spec = tabHost.newTabSpec("movies")
		.setIndicator("", resources.getDrawable(R.drawable.moviestab))
		.setContent(intent);

		tabHost.addTab(spec);


		// We are initializing the TabContent for Sports

		intent = new Intent().setClass(this, Sports.class);
		//TODO - Place the proper image for sports tab icon
		spec = tabHost.newTabSpec("sports")
		.setIndicator("", resources.getDrawable(R.drawable.sportstab))
		.setContent(intent);

		tabHost.addTab(spec);

		tabHost.setCurrentTab(tabHostIndex);

	}

	/**
	 * @return the context
	 */
	public static Context getContext() {
		return context;
	}

	private void startVoiceRecognitionActivity()
	{
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
		startActivityForResult(intent, REQUEST_CODE);
	}

	/**
	 * Handle the results from the voice recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
		{
			// Populate the wordsList with the String values the recognition engine thought it heard
			ArrayList<String> matches = data.getStringArrayListExtra(
					RecognizerIntent.EXTRA_RESULTS);
			//      wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
			//            matches));

			if(matches.isEmpty())
			{
				Toast.makeText(context, "No Match found", 2);
			}
			else
			{
				Boolean flag = false;
				for(int i=0;i<matches.size();i++)
				{
					if(matches.get(i).equalsIgnoreCase("news"))
					{
						Log.d(TAG, "News detected int Vocie Recognition");
						Intent intent = new Intent(context,News3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}
					if(matches.get(i).equalsIgnoreCase("movies"))
					{
						Intent intent = new Intent(context,Movie3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}
					if(matches.get(i).equalsIgnoreCase("sports"))
					{
						Intent intent = new Intent(context,Sports3DActivity.class);
						startActivity(intent);
						flag =true;
						break;
					}


				}
				if(!flag)
				{

					Toast.makeText(context, "No Match found. Try Again!!", Toast.LENGTH_LONG);
					Log.d(TAG, "No match found");
				}

			}

		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {


		Intent intentActivity;
		// Handle item selection

		switch (item.getItemId()) {

		case R.id.threeD:
			if(getLocalClassName().contains("3D")){
				if(getLocalClassName().contains("News")){
					intentActivity = new Intent(getBaseContext(),EntmeActivity.class);	
					intentActivity.putExtra("tabHostIndex", 0);
					startActivity(intentActivity);
				} else if(getLocalClassName().contains("Movies")){
					intentActivity = new Intent(getBaseContext(),EntmeActivity.class);
					intentActivity.putExtra("tabHostIndex", 1);
					startActivity(intentActivity);
				} else if(getLocalClassName().contains("Sports")){
					intentActivity = new Intent(getBaseContext(),EntmeActivity.class);
					intentActivity.putExtra("tabHostIndex", 2);
					startActivity(intentActivity);
				}			    
			}else{

				if(getLocalClassName().equalsIgnoreCase("News")){
					intentActivity = new Intent(getBaseContext(),News3DActivity.class);			
					startActivity(intentActivity);
				} else if(getLocalClassName().equalsIgnoreCase("Movies")){
					intentActivity = new Intent(getBaseContext(),Movie3DActivity.class);			
					startActivity(intentActivity);
				} else if(getLocalClassName().equalsIgnoreCase("Sports")){
					intentActivity = new Intent(getBaseContext(),Sports3DActivity.class);			
					startActivity(intentActivity);
				}	
			}

			return true;			
		case R.id.Settings:
			intentActivity = new Intent(getBaseContext(),Preferences.class);
			startActivity(intentActivity);
			return true;
		case R.id.home:
			return true;
		case R.id.speak:
			startVoiceRecognitionActivity();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
}